package com.ruoyi.back.service.v3.impl

import cn.hutool.core.collection.ListUtil
import cn.hutool.core.util.PageUtil
import cn.hutool.core.util.StrUtil
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.ruoyi.app.common.Data
import com.ruoyi.app.common.Ok
import com.ruoyi.app.common.format
import com.ruoyi.back.domain.v3.SensitiveWord
import com.ruoyi.back.domain.v3.StatusEnum
import com.ruoyi.back.mapper.SensitiveWordMapper
import com.ruoyi.back.service.v3.SensitiveWordService
import com.ruoyi.common.core.page.TableSupport
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.redis.core.StringRedisTemplate
import org.springframework.stereotype.Service

@Service
class SensitiveWordServiceImpl : ServiceImpl<SensitiveWordMapper, SensitiveWord>(), SensitiveWordService {

    @Autowired
    private lateinit var stringRedisTemplate: StringRedisTemplate

    override fun saveSensitiveWord(sensitiveWord: SensitiveWord): Data<Boolean> {

        return sensitiveWord.id?.run {
            Ok(
                "更新成功",
                run {
                    stringRedisTemplate.opsForValue()
                        .set("sensitiveWord-${sensitiveWord.id}", sensitiveWord.sensitiveWord!!.replace("\n", ","))
                    updateById(sensitiveWord)
                }

            )
        } ?: run {
            save(sensitiveWord)
            stringRedisTemplate.opsForValue()
                .set("sensitiveWord-${sensitiveWord.id}", sensitiveWord.sensitiveWord!!.replace("\n", ","))
            Ok("保存成功", true)
        }
    }

    override fun removeSensitiveWord(ids: String): Data<Boolean> {
        return Ok("批量移除成功", run {
            ids.split(",").forEach {
                removeById(it)
                stringRedisTemplate.delete("sensitiveWord-${it}")
            }
            true
        })
    }

    override fun getSensitiveWord(): List<String?>? {
        return list(
            KtQueryWrapper(SensitiveWord::class.java)
                .eq(SensitiveWord::sensitiveWordStatus, StatusEnum.OPEN.code)
        ).takeIf { it.isNotEmpty() }
            ?.run {
                map {
                    stringRedisTemplate.opsForValue().get("sensitiveWord-${it.id}")
                }
            }
    }

    override fun selectSensitiveWord(sensitiveWord: String): Page<SensitiveWord> {


        var sensitiveWords = list(
            KtQueryWrapper(SensitiveWord::class.java)
                .orderByDesc(SensitiveWord::createTime)
        )
            .takeIf { it.isNotEmpty() }
            ?.map {
                it.apply {
                    this.sensitiveWord = stringRedisTemplate.opsForValue().get("sensitiveWord-${it.id}")
                    this.createTimeStr = this.createTime?.format()
                }
            }

        if (StrUtil.isNotBlank(sensitiveWord)) {
            sensitiveWords = sensitiveWords?.filter { StrUtil.isNotBlank(it.sensitiveWord) }
                ?.filter { it.sensitiveWord!!.contains(sensitiveWord) }
        }

        return TableSupport.buildPageRequest().let { pageDomain ->


            sensitiveWords?.let {

                val words = ListUtil.page(
                    pageDomain.pageNum - 1,
                    pageDomain.pageSize,
                    it
                )
                Page<SensitiveWord>().apply {
                    total = it.size.toLong()
                    size = pageDomain.pageSize?.toLong()!!
                    current = pageDomain.pageNum?.toLong()!!
                    records = words
                }
            } ?: Page<SensitiveWord>().apply {
                total = 0
                size = pageDomain.pageSize?.toLong()!!
                current = pageDomain.pageNum?.toLong()!!
            }
        }
    }


}

fun main() {
    println(PageUtil.getStart(1, 10))
    println(PageUtil.getEnd(1, 10))
}